package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.TArticle;
import com.cookor.poi.modules.phy.entity.TArticleType;
import com.cookor.poi.modules.phy.service.TArticleService;
import com.cookor.poi.modules.phy.service.TArticleTypeService;
import com.cookor.poi.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tArticleType"})
public class TArticleTypeController extends BaseController {
  @Autowired
  private TArticleTypeService tArticleTypeService;
  
  @Autowired
  private TArticleService tArticleService;
  
  @ModelAttribute
  public TArticleType get(@RequestParam(required = false) String id) {
    TArticleType entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tArticleTypeService.get(id); 
    if (entity == null)
      entity = new TArticleType(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tArticleType:view"})
  @RequestMapping({"list", ""})
  public String list(TArticleType tArticleType, HttpServletRequest request, HttpServletResponse response, Model model) {
    tArticleType.setTenantId(UserUtils.getTenantId());
    Page<TArticleType> page = this.tArticleTypeService.findPage(new Page(request, response), tArticleType);
    model.addAttribute("page", page);
    model.addAttribute("tArticleType", tArticleType);
    return "modules/phy/tArticleTypeList";
  }
  
  @RequiresPermissions({"phy:tArticleType:view"})
  @RequestMapping({"form"})
  public String form(TArticleType tArticleType, Model model) {
    model.addAttribute("tArticleType", tArticleType);
    model.addAttribute("admin", UserUtils.getUser());
    return "modules/phy/tArticleTypeForm";
  }
  
  @RequiresPermissions({"phy:tArticleType:edit"})
  @RequestMapping({"save"})
  public String save(TArticleType tArticleType, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, tArticleType, new Class[0]))
      return form(tArticleType, model); 
    this.tArticleTypeService.save(tArticleType);
    addMessage(redirectAttributes, new String[] { "保存分类成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tArticleType/?repage";
  }
  
  @RequiresPermissions({"phy:tArticleType:edit"})
  @RequestMapping({"delete"})
  public String delete(TArticleType tArticleType, RedirectAttributes redirectAttributes) {
    TArticle tArticle = new TArticle();
    tArticle.setType(Integer.valueOf(tArticleType.getId()));
    tArticle.setDelFlag("0");
    List<TArticle> list = this.tArticleService.findList(tArticle);
    if (list.size() > 0) {
      addMessage(redirectAttributes, new String[] { "删除分类失败，该分类下还存在文章。" });
      return "redirect:" + Global.getAdminPath() + "/phy/tArticleType/?repage";
    } 
    this.tArticleTypeService.delete(tArticleType);
    addMessage(redirectAttributes, new String[] { "删除分类成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tArticleType/?repage";
  }
  
  @RequiresPermissions({"user"})
  @ResponseBody
  @RequestMapping({"treeData"})
  public List<Map<String, Object>> treeData(@RequestParam(required = false) String typeName, HttpServletResponse response) {
    List<Map<String, Object>> mapList = Lists.newArrayList();
    TArticleType tArticleType = new TArticleType();
    tArticleType.setTypeName(typeName);
    tArticleType.setTenantId(UserUtils.getTenantId());
    List<TArticleType> list = this.tArticleTypeService.findList(tArticleType);
    for (int i = 0; i < list.size(); i++) {
      TArticleType e = list.get(i);
      Map<String, Object> map = Maps.newHashMap();
      map.put("id", e.getId());
      map.put("name", e.getTypeName());
      mapList.add(map);
    } 
    return mapList;
  }
}
